package com.seele.tigerwallet.api;

import android.text.TextUtils;
import android.util.JsonReader;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.seele.base.utils.LogUtils;
import com.seele.tigerwallet.ui.utils.GsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.StringReader;
import java.lang.reflect.ParameterizedType;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hhly211 on 2016/6/28.
 */
public abstract class APICallBack<T> implements Callback<String> {
    protected T data;
    private String dataKey = "data";
    private String codeKey = "code";
    private String msgeKey = "msg";


    @Override
    public void onResponse(Call<String> call, Response<String> response) {

        try {
            if (response.isSuccessful()) {
                String body = response.body();
                if (!TextUtils.isEmpty(body)) {

                    APIModule.setSessioneEror(false);

                    LogUtils.aTag("APICallBack",body);

                    JSONObject jsonObject = new JSONObject(body);
                    if (jsonObject.has(dataKey) && jsonObject.has(codeKey)) {
                        int code = jsonObject.getInt(codeKey);
                        if (code == 0) {
                            String data = jsonObject.getString(dataKey);
                            JsonParser parser = new JsonParser();
                            JsonElement jsonElement = parser.parse(data);
                            if (jsonElement.isJsonArray()) {
                                onArrayResponse(jsonElement);
                            } else if (jsonElement.isJsonObject()) {
                                onObjectResponse(jsonElement);
                            }
                        } else {
                            onResponseError(new APIException(code));
                        }
                    } else if (jsonObject.has(codeKey) && jsonObject.has(msgeKey)) {
                        APIException apiException = GsonUtil.parseJsonWithGson(body, APIException.class);
                        if (apiException.code==APIException.SESSION_INVALID_ERROR||apiException.code==APIException.PARAMETER_ERROR){
                            APIModule.setSessioneEror(true);
                        }

                        onResponseError(GsonUtil.parseJsonWithGson(body,APIException.class));
                    } else {
                        onStringResponse(body);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 返回String
     *
     * @param body
     */
    protected void onStringResponse(String body) {

    }

    /**
     * 响应失败 一半未解析失败,或者服务器返回错误
     *
     * @param e
     */
    protected abstract void onResponseError(APIException e);

    /**
     * 返回对象
     *
     * @param jsonElement
     */

    protected void onObjectResponse(JsonElement jsonElement) {
    }

    /**
     * 返回数组
     *
     * @param jsonElement
     */
    protected void onArrayResponse(JsonElement jsonElement) {

    }

    /**
     * 请求超时 或者未知错误
     *
     * @param call
     * @param t
     */
    @Override
    public void onFailure(Call<String> call, Throwable t) {
        t.printStackTrace();
        onFailureError(call, t);


    }

    protected abstract void onFailureError(Call<String> call, Throwable t);

    public Class<T> getType() {
        ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
        return (Class<T>) type.getActualTypeArguments()[0];
    }

}
